import os
from urllib import request

from flask import Flask, render_template, flash
from werkzeug.utils import secure_filename

from geo import geocode, around
from weather import weather



app = Flask(__name__)



# 首页homepage
@app.route('/')
@app.route('/login')
def login_page():
	return render_template('login.html')


@app.route('/homepage')
def homepage_page():
	return render_template('homepage.html')

@app.route('/introduce')
def introduce_page():
	return render_template('introduce.html')

@app.route('/weather')
def weather_page():
	return render_template('weather.html')

#@app.route('/skill')
#def skill_page():
	#return render_template('skill.html')

@app.route('/interest')
def interest_page():
	return render_template('interest.html')

@app.route('/awards')
def awards_page():
	return render_template('awards.html')

# 城市查询页面
@app.route('/skill')
def skill_page() -> 'html':
	return render_template("skill.html",
							the_title="Welcome to the map")

# 点击“查询”后，利用高德api-geocode和circum数据返回results
@app.route('/geocode', methods=['POST'])
def geo() -> 'html':
	address = request.form['address']
	codes = str(geocode(address)['geocodes'][0][ 'location'])

	location = codes
	types = request.form['types']
	results = around(location,types)['pois']


	names = []
	address2 = []
	for i in results:
		names.append(i['name'])
		address2.append(i['address'])
	combine = dict(zip(names,address2))

	#log_request(request,results)
	return render_template('skill.html',
							res=combine,
							the_title='以下为结果：')

# 分析用户所上传图片
@app.route('/ground', methods=['POST','GET'])
def ground_data_page() -> 'html':
	if request.method == 'POST':
		f = request.files.get('selectfile')
		if not os.path.exists('uploadDir'):
			os.makedirs('uploadDir')
		if f:
			filename = secure_filename(f.filename)
			types = ['jpg', 'png', 'tif']
			if filename.split('.')[-1] in types:
				uploadpath = os.path.join('uploadDir', filename)
				f.save(uploadpath)
				flash('Upload Load Successful!', 'success')
				return render_template('interest.html', imagename=filename)
			else:
				flash('Unknown Types!', 'danger')
		else:
			flash('No File Selected.', 'danger')
	return render_template('interest.html')

# 广州天气查询
@app.route("/tianqi",methods=["post"])
def tianqi():
	key='030d60847cc2dbf652a0d1f7e0fce690'
	city='440100'
	extensions='base'
	a=weather(key,city,extensions)
	b=a['lives'][0]['city']
	c=a['lives'][0]['weather']
	d=a['lives'][0]['temperature']
	return render_template('weather.html',city=str(b),weather=str(c),temperature=str(d))




if __name__ == '__main__':
    app.run()
